package com.kaussoft.lolblog.util;

import com.opensymphony.webwork.util.WebWorkTypeConverter;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Map;

public class DateConverter extends WebWorkTypeConverter {
	@Override
	public Object convertFromString(Map map, String[] arg, Class clazz) {
		if (clazz != Date.class) {
			return null;
		}

		Date date = null;

		try {
			date = new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(arg[0]);
		} catch (ParseException e) {
			try {
				date = new SimpleDateFormat("yyyy-MM-dd").parse(arg[0]);
			} catch (ParseException e1) {
				date = null;
			}
		}

		return date;
	}

	@Override
	public String convertToString(Map map, Object obj) {
		String s = null;

		if (obj instanceof Date) {
			s = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(obj);
		}

		return s;
	}
}
